import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

// This class will get called when the user successfully logs in.
// This class will return the list of the courses that are currently offered 
public class StudentsEnrolled extends HttpServlet{
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/plain");
		PrintWriter pw = response.getWriter();
		String courseCode = request.getParameter("courseCode");
		String user_name = request.getParameter("userID");
		//connect to the database
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		int result = 0;
		String query = "SELECT DISTINCT user.id, user.first_name,user.last_name from user, course_enrolled where user.id=course_enrolled.user_name and course_code = \""+courseCode+"\" and user_name <> \""+user_name+"\"";
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection("jdbc:mysql://bungle08.cs.toronto.edu:3306/group22",
			"group22", "123456");
	
			if(!con.isClosed()){
				stmt = con.createStatement();		
				rs = stmt.executeQuery(query);			
					
				while (rs.next()) {
					pw.println(rs.getString("id"));
					pw.println(rs.getString("first_name"));
					pw.println(rs.getString("last_name"));
				}
			}
		}
		catch(Exception e) {
			pw.println("Exception: " + e.getMessage());
		} finally {
			try {
				if(con != null)
					con.close();
			} catch(SQLException e) {}
		}
	}
}